package common

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/gin-gonic/gin"
)

// func SendFilad(c *gin.Context, httpCode int, code int, message string) {
// 	c.JSON(httpCode, NewResponse(code, message))
// }

func NewResponse(ctx context.Context, code int, HttppCode int, message string) *response {
	//nil状态未知 没传值默认wei0
	if code == 0 {
		return &response{Code: nil, HttpCode: HttppCode, Message: message}
	}
	return &response{Code: &code, HttpCode: HttppCode, Message: message}
}

func ReturnReponse(c *gin.Context, msg string, code int, httpcode int) {
	resp := NewResponse(c, http.StatusBadRequest, http.StatusBadRequest, msg)
	c.JSON(http.StatusBadRequest, resp)
}

// 配置http状态码
func (r *response) SetHttpCode(code int) {
	r.HttpCode = code
}

type response struct {
	Code     *int   `json:"code"`
	HttpCode int    `json:"http_code"`
	Message  string `json:"message"`
}

func (r *response) Error() string {
	return r.Message
}

// 结构体输出格式定义 实现stringer接口
func (r *response) String() string {
	d, _ := json.Marshal(r)
	return string(d)
}
